import org.codehaus.groovy.runtime.DefaultGroovyMethods
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
import org.junit.jupiter.api.Test

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class BasicTest{

    @Test
    public void evalTest(){
        assert Eval.me('33*3') == 99
        assert Eval.me('"foo".toUpperCase()') == 'FOO'
    }
    @Test
    public void cc(){

    }

    int method(String arg) {
        return 1;
    }
    int method(Object arg) {
        return 2;
    }

    @Test
    public void multiMethodTest(){
        Object o = "Object"; // groovy in the static stage to compile  , and ensure the type is string ,
        int result = method(o);
        assert result==1
    }
    @Test
    public void arrayTest(){
        def array2 = new int[] {1, 2, 3}


    }
    @Test
    public void constructTest(){
//        def aaa = ${['online', 'offline']}_table${1..3};
        def range = 1..3;
        assert range[0]==1;

    /**
     * range type to iterate ,
     */
        def table = (1..3).each {it
            println(it)
        }

    }

    @Test
    public void writeFileTestTwo(){
        String baseDir="F:\\";
        File file=new File(baseDir,'haiku.txt');
        file.bytes = [66,22,11];
    }
    @Test
    public void writeFileTest(){
        String baseDir="F:\\";
        new File(baseDir,'haiku.txt').withWriter('utf-8') { writer ->
            writer.writeLine 'Into the ancient pond'
            writer.writeLine 'A frog jumps'
            writer.writeLine 'Water’s sound!'
        }
    }
    @Test
    public void collectFromFileTest(){
        String filename='F:\\integration\\apache\\groovy\\src\\bin\\groovy.bat';
        def file= new File(filename);
        def list = file.collect {it}
        def array = file as String[]
        byte[] contents = file.bytes;

        file.withInputStream { stream ->
            // do something ...
//            default.sum([1,2,3])
            assert DefaultGroovyMethods.sum([1,2,3])==6;
            this.&print(stream);
        }


    }
    @Test
    public void fileForEach(){
        new File('F:\\integration\\apache\\groovy\\src\\bin\\groovy.bat').eachLine('UTF-8') {
//            println it
            this.&println(it)
        }
    }
    @Test
    public void timeTaskTest(){
        CountDownLatch called = new CountDownLatch(1)
        Timer timer = new Timer()
        timer.schedule(new TimerTask() {
            void run() {
                called.countDown()
            }
        }, 0)

        assert called.await(10, TimeUnit.SECONDS)
    }
    @Test
    public void innerClassTest(){
        def a = new CustomA();
        CustomA.X x = a.createX();
        x.sayHello()
    }
    @Test
    public void lambdaTest(){
    }
    @Test
    public void GstringVsCharacterLiterals(){
        assert 'c'.getClass()==String
        assert "c".getClass()==String
        assert "c${1}".getClass() in  GString
    }
    @Test
    public void GstringVsCharacterLiteralsTestOne(){
        char a='a'
        assert Character.digit(a, 16)==10 : 'But Groovy does boxing'
        assert Character.digit((char) 'a', 16)==10

        try {
            assert Character.digit('a', 16)==10
            assert false: 'Need explicit cast'
        } catch(MissingMethodException e) {
        }
    }
    @Test
    public void GstringVsCharacterLiteralsTestTwo(){
        // for single char strings, both are the same
        assert ((char) "c").getClass()==Character
        assert ("c" as char).class==Character

        // for multi char strings they are not
        try {
          ((char) 'cx') == 'c'
          assert false: 'will fail - not castable'
        } catch(GroovyCastException e) {
        }
        assert ('cx' as char) == 'c'
        assert 'cx'.asType(char) == 'c'
    }
    @Test
    public void equalsTest(){
        def  cus = new CustomA("cj",1);
        assert cus==new CustomA("hcj",1);

    }
    @Test
    public void primitivesAndWrapper(){
        def  cus = new CustomA("cj",1);
        assert  cus.z1.equals(0.1f);

        def bigDecimal = new BigDecimal(10,2);
        bigDecimal.doubleValue();
        Float aFloat =1f;
        aFloat.doubleValue();
        def value = Number.doubleValue();
        assert 'c' as char;



    }



}


